<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" @Launchform="Launchform12" :formList="formList2" @findClicked="findData" />

    <div class="ProductionDynamic">
      <!-- 主体 -->
      <div class="ProductionDynamicMain">
        <el-card class="ProductionDynamicMainCard" shadow="always" :body-style="{ padding: '15px' }">
          <div class="ProductionDynamicMainCardMain">
            <div style="width: 20%">
              <h1 class="H1Size">
                冲刀资料<el-button type="primary" size="small" @click="dialogVisible2 = true">增</el-button>
                <el-button type="primary" size="small" @click="handleCloseMateriaLKlc">删</el-button>
              </h1>
              <Table
                :isLoading="isLoading"
                @inputChange="checkboxChange1"
                @currentRow="selectList"
                :tableData="PunchingDataQueryTableDate"
                :tableHeader="tableHeader"
                :selection="false"
                :TableHeight="100"
                :outTableHeight="50"
              />
            </div>
            <div style="width: 30%; margin-left: 15px">
              <div>
                <h1 class="H1Size">
                  冲刀部位资料<el-button type="primary" size="small" @click="dialogVisible = true">增</el-button>
                  <el-button type="primary" size="small" @click="handleCloseMateriaLKlc8">删</el-button>
                </h1>
                <Table
                  :isLoading="isLoading1"
                  @inputChange="checkboxChange3"
                  @currentRow="selectList3"
                  :tableData="PunchingPartQueryMethodsTableDate"
                  :tableHeader="tableHeader1"
                  :selection="false"
                  :TableHeight="100"
                  :outTableHeight="50"
                />
              </div>
            </div>
            <div style="width: 17%; margin-left: 15px">
              <div>
                <h1 class="H1Size">
                  冲刀所用型体资料
                  <el-button type="primary" size="small" @click="handleCloseMateriaLKlc1">删</el-button>
                </h1>
                <Table
                  :isLoading="isLoading2"
                  @inputChange="checkboxChange"
                  @currentRow="selectList2"
                  :tableData="TypeUsedForPunchingKnivesTableDate"
                  :tableHeader="tableHeader2"
                  :selection="false"
                  :TableHeight="100"
                  :outTableHeight="50"
                />
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <el-dialog v-model="dialogVisible2" width="50%">
      <Find :OriginalFilter="{ static: true }" @Launchform="Launchform1" @findClicked="findClicked12" :searchImg="false" :formList="formList" />
    </el-dialog>

    <el-dialog v-model="dialogVisible3" width="50%">
      <Find :OriginalFilter="{ static: true }" @findClicked="findData1" :searchImg="false" @Launchform="Launchform13" :formList="formList1" />
      <Table
        :isLoading="isLoading3"
        @selectList="selectList1"
        style="width: 300px"
        :tableData="styleQueryTableDate"
        :tableHeader="tableHeader3"
        :selection="true"
        :TableHeight="100"
        :outTableHeight="40"
      />
      <PageModule
        :PageData="{ total: totalA, buttonS: true, pageSizeShow: true }"
        :layout="'prev, pager'"
        @pageSizeChange="handleSizeChange1"
        @pageChange="handleCurrentChange1"
      />
    </el-dialog>

    <el-dialog v-model="dialogVisible" width="50%">
      <Find :OriginalFilter="{ static: true }" @Launchform="Launchform" @findClicked="findClicked" :searchImg="false" :formList="formList3" />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'PunchingData'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { styleQuery } from '@/api/Control/FactoryControl/LastInformation'
import {
  DeletePunchingPartQuery,
  PunchingDataQuery,
  PunchingPartQuery,
  TypeUsedForPunchingKnives,
  AddPunchingData,
  deletePunchingDate,
  AddCutStyle,
  DeleteCutStyle,
  AddPunchingPartQuery
} from '@/api/Control/FactoryControl/PunchingData'
import LoginWork from '@/utils/LoginWork'
import { ElMessage, ElMessageBox } from 'element-plus'
const isLoading = ref(false)
const isLoading1 = ref(false)
const isLoading2 = ref(false)
const isLoading3 = ref(false)

// 删除
const slipFlagList = ref('')

const DeletePunchingPartQueryMethods = async () => {
  const result = await DeletePunchingPartQuery(cutNoList.value, slipFlagList.value, partNameOldList.value)
  if (result.code === 200) {
    PunchingPartQueryMethods()
    slipFlagList.value = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlc8 = () => {
  if (slipFlagList.value === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        DeletePunchingPartQueryMethods()
      })
      .catch(() => {})
  }
}

// 冲刀部位更新
const partNameOldList = ref('')

const selectList3 = (row: any) => {
  partNameOldList.value = row.PART_NAME
  slipFlagList.value = row.slipFlag
}

const checkboxChange3 = async (row: any) => {
  row.cutNo = cutNoList.value
  row.partName = row.PART_NAME
  row.remark = row.REMARK
  row.seqNo = row.SEQ_NO
  row.sizeRegion = row.SIZE_REGION
  const result = await LoginWork.post<any, any>(
    `/bioDeptWrok/cpCutDetUp?slipFlag=${row.slipFlag}&cutNoOld=${cutNoList.value}&partNameOld=${partNameOldList.value}`,
    row
  )
  if (result.code === 200) {
    PunchingPartQueryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 冲刀部位新增
const dialogVisible = ref(false)

const Launchform = (row: any) => {
  AddPunchingPartQueryParms.value.seqNo = Number(row[0].inputValue)
  AddPunchingPartQueryParms.value.partName = row[1].inputValue
  AddPunchingPartQueryParms.value.sizeRegion = row[2].inputValue
  AddPunchingPartQueryParms.value.remark = row[3].inputValue
}

const clearFormList = () => {
  formList3.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    // Add other conditions for different form item types
  })
}

const formList3 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入序号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入部位',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入号码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入备注',
    inputValue: ''
  },

  {
    type: 'button',
    checkboxLabel: '保存'
  }
])

const AddPunchingPartQueryParms = ref({
  cutNo: '',
  partName: '',
  remark: '',
  seqNo: -5,
  sizeRegion: ''
})

const findClicked = (row: string) => {
  if (row === '保存') {
    if (AddPunchingPartQueryParms.value.seqNo === -5) {
      JMessage.warning('序号不可为空!')
    } else {
      AddPunchingPartQueryMethods()
    }
  }
}

const AddPunchingPartQueryMethods = async () => {
  AddPunchingPartQueryParms.value.cutNo = cutNoList.value
  const result = await AddPunchingPartQuery(AddPunchingPartQueryParms.value)
  if (result.code === 200) {
    PunchingPartQueryMethods()
    dialogVisible.value = false
    AddPunchingPartQueryParms.value.seqNo = -5
    clearFormList()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const checkbostyle = ref('')

const checkboxChange = async (row: any) => {
  row.cutNo = cutNoList.value
  const result = await LoginWork.post<any, any>(`/bioDeptWrok/cpCutStyleUp?styleOld=${checkbostyle.value}&cutNoOld=${cutNoList.value}`, row)
  if (result.code === 200) {
    TypeUsedForPunchingKnivesMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 冲刀删除
const styleListAny = ref('')

const selectList2 = (row: any) => {
  styleListAny.value = row.style
  checkbostyle.value = row.style
}

const DeleteCutStyleMethods = async () => {
  const result = await DeleteCutStyle(cutNoList.value, styleListAny.value)
  if (result.code === 200) {
    TypeUsedForPunchingKnivesMethods()
    styleListAny.value = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlc1 = () => {
  if (styleListAny.value === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        DeleteCutStyleMethods()
      })
      .catch(() => {})
  }
}

// 冲刀所用型体新增
const tableHeader3 = ref([
  {
    label: '型体',
    prop: 'style'
  }
])

const dialogVisible3 = ref(false)

const HolidayInquiryParms = ref({
  pageNum: 1,
  pageSize: 40
})

const Launchform13 = (row: any) => {
  styleList1.value = row[0].inputValue
  styleQueryMethods()
}

const styleList1 = ref('')

const styleQueryTableDate = ref([])

const styleQueryMethods = async () => {
  isLoading3.value = true
  const result = await styleQuery(HolidayInquiryParms.value, styleList1.value)

  styleQueryTableDate.value = result.dtos.map((item: any) => {
    return { style: item }
  })
  totalA.value = result.total
  isLoading3.value = false
}

const totalA = ref(0)

const handleSizeChange1 = (val: number) => {
  HolidayInquiryParms.value.pageSize = val
  styleQueryMethods()
}

const handleCurrentChange1 = (val: number) => {
  HolidayInquiryParms.value.pageNum = val
  styleQueryMethods()
}

const formList1 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '保存'
  }
])

const AddCutStyleMethodsParms = ref<any>([])

const selectList1 = (row: any) => {
  AddCutStyleMethodsParms.value = row.map((item: any) => {
    return item.style
  })
}

const findData1 = (row: string) => {
  if (row === '保存') {
    if (AddCutStyleMethodsParms.value.length === 0) {
      JMessage.warning('请选择需要保存的新增的型体')
    } else {
      AddCutStyleMethods()
    }
  }
}

const AddCutStyleMethods = async () => {
  const result = await AddCutStyle(cutNoList.value, AddCutStyleMethodsParms.value)
  if (result.code === 200) {
    AddCutStyleMethodsParms.value = []
    dialogVisible3.value = false
    TypeUsedForPunchingKnivesMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 冲刀资料修改
const checkboxChange1 = async (list: any) => {
  list.techNote = list.TECH_NOTE
  list.cutNo = list.CUT_NO
  const result = await LoginWork.post<any, any>(`/bioDeptWrok/cpCutUp?slipFlag=${slipFlagA.value}&cutNoOld=${cutNoList.value}`, list)
  if (result.code === 200) {
    PunchingDataQueryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 冲刀资料删除
const cutNoA = ref('')

const slipFlagA = ref('')

const deletePunchingDateMethods = async () => {
  const result = await deletePunchingDate(cutNoA.value, slipFlagA.value)
  if (result.code === 200) {
    PunchingDataQueryMethods()
    cutNoA.value = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlc = () => {
  if (cutNoA.value === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        deletePunchingDateMethods()
      })
      .catch(() => {})
  }
}

// 冲刀资料新增
const dialogVisible2 = ref(false)

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入冲刀号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入备注',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '保存'
  }
])

const AddPunchingDataMethodsParms = ref({
  cutNo: '',
  techNote: ''
})

const findClicked12 = (count: string) => {
  if (count === '保存') {
    if (AddPunchingDataMethodsParms.value.cutNo === '') {
      JMessage.warning('冲刀号不可为空')
    } else {
      AddPunchingDataMethods()
    }
  }
}

const clearFormList2 = () => {
  formList.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    // Add other conditions for different form item types
  })
}

const Launchform1 = (row: any) => {
  AddPunchingDataMethodsParms.value.cutNo = row[0].inputValue
  AddPunchingDataMethodsParms.value.techNote = row[1].inputValue
}

const AddPunchingDataMethods = async () => {
  const result = await AddPunchingData(AddPunchingDataMethodsParms.value)
  if (result.code === 200) {
    dialogVisible2.value = false
    PunchingDataQueryMethods()
    clearFormList2()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 查询
const Launchform12 = (row: any) => {
  styleList.value = row[0].inputValue
  PunchingDataQueryMethods()
}

// 冲刀所用型体资料
const TypeUsedForPunchingKnivesTableDate = ref([])

const TypeUsedForPunchingKnivesMethods = async () => {
  isLoading2.value = true
  const result = await TypeUsedForPunchingKnives(cutNoList.value)
  TypeUsedForPunchingKnivesTableDate.value = result.map((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
    return item
  })
  isLoading2.value = false
}

// 冲刀部位资料查询
const cutNoList = ref('')

const selectList = (row: any) => {
  cutNoList.value = row.CUT_NO
  cutNoA.value = row.CUT_NO
  slipFlagA.value = row.slipFlag
  PunchingPartQueryMethods()
  TypeUsedForPunchingKnivesMethods()
}

const PunchingPartQueryMethodsTableDate = ref([])

const PunchingPartQueryMethods = async () => {
  isLoading1.value = true
  const result = await PunchingPartQuery(cutNoList.value)
  PunchingPartQueryMethodsTableDate.value = result.map((item: any) => {
    if (item.slipFlag === 'Y') {
      item.backgroundColor = '#00ff00'
    } else {
      item.change = 'Y'
      item.disableConfirm = true
    }
    return item
  })
  isLoading1.value = false
}

// 冲刀资料查询
const styleList = ref('')

const PunchingDataQueryTableDate = ref([])

const PunchingDataQueryMethods = async () => {
  isLoading.value = true
  const result = await PunchingDataQuery(styleList.value)
  PunchingDataQueryTableDate.value = result.map((item: any) => {
    if (item.slipFlag === 'Y') {
      item.backgroundColor = '#00ff00'
    } else {
      item.change = 'Y'
      item.disableConfirm = true
    }
    return item
  })
  if (result.length > 0) {
    cutNoList.value = result[0].CUT_NO
    PunchingPartQueryMethods()
    TypeUsedForPunchingKnivesMethods()
  }
  isLoading.value = false
}

onMounted(() => {
  PunchingDataQueryMethods()
})

const formList2 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '查询资料'
  },
  {
    type: 'button',
    checkboxLabel: '取部位资料'
  },
  {
    type: 'button',
    checkboxLabel: '更新型体'
  },
  {
    type: 'button',
    checkboxLabel: '选择型体'
  }
])

const findData = (data: any) => {
  if (data === '选择型体') {
    dialogVisible3.value = true
    styleQueryMethods()
  }
}

const tableHeader = ref<any>([
  {
    label: '冲刀号',
    prop: 'CUT_NO',
    backgroundColor: true
  },
  {
    label: '备注',
    prop: 'TECH_NOTE'
  }
])

// 表格数据

const tableHeader1 = ref([
  {
    label: '序号',
    prop: 'SEQ_NO'
  },
  {
    label: '部位',
    prop: 'PART_NAME',
    backgroundColor: true
  },
  {
    label: '号码',
    prop: 'SIZE_REGION'
  },
  {
    label: '备注',
    prop: 'REMARK'
  }
])

const tableHeader2 = ref([
  {
    label: '型体',
    prop: 'style'
  }
])
</script>

<style lang="less" scoped>
.ProductionDynamicMainCardMain {
  display: flex;
}

.H1Size {
  font-size: 16px;
  justify-content: space-between;
}

.bottom {
  float: right;
  margin-top: 15px;
}

.ProductionDynamicMainCardMain {
  display: flex;
}

.ProductionDynamic {
  margin-top: 15px;
}
</style>
